package com.newtouch.avmarketing.mgr.modules.avmarkrting.api;

import cn.hutool.core.lang.Assert;
import com.newtouch.avmarketing.mgr.modules.avmarkrting.config.VideoResourceHander;
import com.newtouch.avmarketing.mgr.modules.avmarkrting.entity.DoubleAuditing;
import com.newtouch.avmarketing.mgr.modules.avmarkrting.entity.VideoInformation;
import com.newtouch.avmarketing.mgr.modules.avmarkrting.entity.dto.DoubleAuditingDto;
import com.newtouch.avmarketing.mgr.modules.avmarkrting.model.DoubleAuditingCondition;
import com.newtouch.avmarketing.mgr.modules.avmarkrting.model.Result;
import com.newtouch.avmarketing.mgr.modules.avmarkrting.model.ResultCodes;
import com.newtouch.avmarketing.mgr.modules.avmarkrting.rest.AnonymousGetMapping;
import com.newtouch.avmarketing.mgr.modules.avmarkrting.service.DoubleAuditingDtoService;
import com.newtouch.avmarketing.mgr.modules.avmarkrting.service.DoubleAuditingService;
import com.newtouch.avmarketing.mgr.modules.avmarkrting.service.VideoInformationService;
import com.newtouch.avmarketing.mgr.modules.avmarkrting.service.VideoPlayService;
import com.newtouch.avmarketing.mgr.modules.avmarkrting.util.ExportUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

@Api(value = "双录配置API", tags = "03 双录管理：双录审核")
@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/api/doubleAuditing")
public class DoubleAuditingApi {
    @Autowired
    private DoubleAuditingService doubleAuditingService;
    @Autowired
    private DoubleAuditingDtoService doubleAuditingDtoService;
    @Autowired
    private VideoInformationService videoInformationService;
    @Autowired
    private VideoPlayService videoPlayService;

    @ApiOperation("查询双录视频节点信息")
    @PostMapping("/findByPolicyCode")
    public Result findByPolicyCode(@ApiParam("policyCode") @RequestParam String policyCode){
        List<VideoInformation> byPolicyCode = videoInformationService.findByPolicyCode(policyCode);
        if (byPolicyCode.size()!=0){
            return Result.of(ResultCodes.SUCCESS_CODE,"查询成功",byPolicyCode);
        }else {
            log.error("查询失败");
        }
        return Result.of(ResultCodes.ERROR_CODE,"查询失败");
    }

    @ApiOperation("投保单号查询双录审核信息")
    @PostMapping("/findDoubleAuditingDto")
    public Result findDoubleAuditingDto(@ApiParam("policyCode") @RequestParam String policyCode){
        try {
            DoubleAuditingDto doubleAuditingDto = doubleAuditingDtoService.findDoubleAuditingDto(policyCode);
            if (doubleAuditingDto != null){
                return Result.of(ResultCodes.SUCCESS_CODE,"查询成功",doubleAuditingDto);
            }
        }catch (Exception e){
            e.printStackTrace();
            log.error("查询失败",e);
        }
        return Result.of(ResultCodes.ERROR_CODE,"查询失败");
    }

    @ApiOperation("双录视频审核不通过")
    @PostMapping("/updateDoubleAuditing")
    public Result updateDoubleAuditing(@ApiParam("policyCode") @RequestParam String policyCode){
        try {
            doubleAuditingService.updateDoubleAuditing(policyCode);
            return Result.of(ResultCodes.SUCCESS_CODE,"需要重录");
        }catch (Exception e){
            log.error("需要重录失败");
        }
        return Result.of(ResultCodes.ERROR_CODE,"需要重录失败! ");
    }
    @ApiOperation("双录视频审核通过")
    @PostMapping("/updateDoubleAuditingPass")
    public Result updateDoubleAuditingPass(@ApiParam("policyCode") @RequestParam String policyCode){
        try {
            doubleAuditingService.updateDoubleAuditingPass(policyCode);
            return Result.of(ResultCodes.SUCCESS_CODE,"审核通过");
        }catch (Exception e){
            log.error("审核通过失败");
        }
        return Result.of(ResultCodes.ERROR_CODE,"审核通过失败");
    }

    @ApiOperation("条件查询双录审核信息")
    @PostMapping("/findDoubleAuditing")
    public Result findDoubleAuditing(@RequestBody DoubleAuditingCondition doubleAuditingCondition){
        try {
            Page<DoubleAuditing> list = doubleAuditingService.findDoubleAuditing(doubleAuditingCondition);
            if (null != list){
                return Result.of(ResultCodes.SUCCESS_CODE,"查询成功! ",list);
            }
        }catch (Exception e){
            log.error("错误信息：",e);
        }
        return Result.of(ResultCodes.ERROR_CODE,"查询失败! ");
    }

    private final VideoResourceHander videoResourceHander;

    public DoubleAuditingApi(VideoResourceHander videoResourceHander) {
        this.videoResourceHander = videoResourceHander;
    }

    /**
     * 查询视频地址并播放
     * @param response
     * @param policyCode
     * @throws Exception
     */
    @ApiOperation("查询视频地址并播放")
    @AnonymousGetMapping("/url/{policyCode}")
    public void videoInfo(HttpServletResponse response, HttpServletRequest request,
                          @ApiParam("policyCode") @PathVariable String policyCode) throws Exception{
        try{
            String filePathStr = videoPlayService.getFilePath(policyCode);
            log.info("------{},---{}",policyCode,filePathStr);
            Assert.notBlank(filePathStr);
            Path filePath = Paths.get(filePathStr);
            request.setAttribute(videoResourceHander.ATTR_FILE, filePath);

            videoResourceHander.handleRequest(request,response);

            log.info("播放成功");
        }catch (Exception e){
            log.error("播放失败",e);
        }
    }
    /**
     * 导出数据
     */
    @ApiOperation("导出双录审核信息")
    @PostMapping("/doubleAuditingExport")
    public void doubleAuditingExport(HttpServletResponse response, @RequestBody DoubleAuditingCondition doubleAuditing)
            throws Exception {
        try{
            Page<DoubleAuditing> doubleAuditing1 = doubleAuditingService.findDoubleAuditing(doubleAuditing);
            ExportUtil.getExport(response,doubleAuditing1.getContent());
        }catch (Exception e){
            log.error("错误信息：",e);
        }
    }
}
